<?php
/**
 * 登录，登出
 */
namespace backend\modules\admin\controllers;

use Yii;
use backend\modules\admin\models\AdminForm;
use backend\modules\admin\models\Role;
use yii\helpers\Url;

/**
 * Site controller
 */
class LoginController extends Controller
{
    /**
     * 登录
     * @return string|\yii\web\Response
     */
    public function actionLogin()
    {
         $admin=new AdminForm();
         $param=Yii::$app->request->post();

         if($param)
         {
             $data = $param['AdminForm'];
             $admin->load($param);
           
            if($admin->login())
            {
                $user=$admin->getAdmin();
                
                $user_status=Role::find()->select('status')->where(['roleid'=>$user->roleid])->one();
                if($user_status->status==0)
                {
                    if($user->getIpTime($data['username']))
                    {
                        return $this->redirect(Url::to(['/admin/admin/index']));
                    }
                    else
                    {
                        return $this->render('//msg',
                            [
                                'url'=>Url::to('@web/admin/login/login'),//跳转页面，若为goback,则返回上一页
                                'msg'=>'登陆信息录入失败，请重新登陆！',//跳转自定义信息
                                'ms'=>1000,//延迟时间
                                'msg_type'=>'error'//消息类型，success,error,close
                            ]
                        );
                    }
                }
                else
                {
                    return $this->render('//msg',
                        [
                            'url'=>Url::to('@web/admin/login/login'),//跳转页面，若为goback,则返回上一页
                            'msg'=>'该用户角色已失效，请联系管理员！',//跳转自定义信息
                            'ms'=>1000,//延迟时间
                            'msg_type'=>'erroe'//消息类型，success,error,close
                        ]
                    );
        
                }
                
            }
            else
            {
                $this->layout=false;
                return $this->render('//msg',
                    [
                        'url'=>Url::to('@web/admin/login/login'),
                        'msg'=>'账号或密码错误，请重新登录!',
                        'ms'=>1000,
                        'msg_type'=>'error'
                    ]);
            }
         }
         else
         {
             return  $this->renderPartial('/login',['model'=>$admin]);
         }
     
    }

    //注销登陆
    public function actionLogout()
    {
        Yii::$app->session['adminuser']=null;
        return  $this->redirect(['login']);
    }
   
}
    
